import request from '../utils/request'
import type {
  User,
  Department,
  Position,
  UserApiResponse as ApiResponse,
  UserPaginationResponse as PaginationResponse,
  UserListParams,
} from '../types/user'

export const userApi = {
  // 用户管理相关接口
  // 获取用户列表
  getUserList: (params: UserListParams): Promise<PaginationResponse<User>> => {
    return request({
      url: '/api/user/user/index',
      method: 'get',
      params,
    })
  },

  // 查看用户详情
  viewUser: (params?: any) => {
    return request({
      url: '/api/user/user/show',
      method: 'get',
      params,
    })
  },

  // 获取用户选项（用于下拉选择）
  getUserOptions: (params?: any) => {
    return request({
      url: '/api/user/user/options',
      method: 'get',
      params,
    })
  },

  // 新增用户
  addUser: (data: any) => {
    return request({
      url: '/api/user/user/add',
      method: 'post',
      data,
    })
  },

  // 编辑用户
  editUser: (data: any) => {
    return request({
      url: '/api/user/user/update',
      method: 'put',
      data,
    })
  },

  // 删除用户
  deleteUser: (data: any) => {
    return request({
      url: '/api/user/user/destroy',
      method: 'delete',
      data,
    })
  },

  // 重置用户密码
  resetUserPassword: (data: any) => {
    return request({
      url: '/api/user/user/reset_password',
      method: 'post',
      data,
    })
  },

  // 修改用户密码
  changePassword: (data: { old_password: string; new_password: string }) => {
    return request({
      url: '/api/user/user/change_password',
      method: 'post',
      data,
    })
  },

  // 禁用/恢复用户
  setUserStatus: (data: any) => {
    return request({
      url: '/api/user/user/set',
      method: 'post',
      data,
    })
  },

  // 部门管理相关接口
  // 获取部门列表（树形结构）
  getDepartmentList: (
    params?: any
  ): Promise<ApiResponse<Department[]> | PaginationResponse<Department>> => {
    return request({
      url: '/api/user/department/list',
      method: 'get',
      params,
    })
  },

  // 查看部门详情
  viewDepartment: (params?: any) => {
    return request({
      url: '/api/user/department/view',
      method: 'get',
      params,
    })
  },

  // 获取部门选项（用于下拉选择）
  getDepartmentOptions: (params?: any) => {
    return request({
      url: '/api/user/department/options',
      method: 'get',
      params,
    })
  },

  // 新增部门
  saveDepartment: (data: any) => {
    return request({
      url: '/api/user/department/add',
      method: 'post',
      data,
    })
  },

  // 编辑部门
  editDepartment: (data: any) => {
    return request({
      url: '/api/user/department/edit',
      method: 'put',
      data,
    })
  },

  // 删除部门
  deleteDepartment: (data: any) => {
    return request({
      url: '/api/user/department/del',
      method: 'delete',
      data,
    })
  },

  // 岗位管理相关接口
  // 获取职位列表
  getPositionList: (params?: any): Promise<PaginationResponse<Position>> => {
    return request({
      url: '/api/user/position/index',
      method: 'get',
      params,
    })
  },

  // 查看职位详情
  viewPosition: (params?: any) => {
    return request({
      url: '/api/user/position/show',
      method: 'get',
      params,
    })
  },

  // 获取职位选项（用于下拉选择）
  getPositionOptions: (params?: any) => {
    return request({
      url: '/api/user/position/options',
      method: 'get',
      params,
    })
  },

  // 新增职位
  addPosition: (data: any) => {
    return request({
      url: '/api/user/position/add',
      method: 'post',
      data,
    })
  },

  // 编辑职位
  editPosition: (data: any) => {
    return request({
      url: '/api/user/position/edit',
      method: 'put',
      data,
    })
  },

  // 删除职位
  deletePosition: (data: any) => {
    return request({
      url: '/api/user/position/del',
      method: 'delete',
      data,
    })
  },
}
